//
//  BanBu_EmoStoreCell.m
// Halfarea
//
//  Created by Wang Xj on 14-3-25.
//
//

#import "BanBu_EmoStoreCell.h"
#import "BanBu_SmileViewAPI.h"
#import "AppCommunicationManager.h"
#import "AppDataManager.h"
#import "UIImage+emoImage.h"
#import "UIImageView+WebCache.h"

#import "BanBu_ImageView.h"
#import "BanBu_ThemeManager.h"

@implementation BanBu_EmoStoreCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
   
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)dealloc
{
    [_newImageView release];
    [_faceImageView release];
    [_nameLabel release];
    [_introductionLabel release];
    [_downloadBtn release];
    [_lineView release];
    [_peogress release];
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:KFinishDownloadingEmo object:nil];
    
    [super dealloc];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _lineView.image = [ThemeManager getThemeImage:@"lineColor.png"];
}

//设置cell的各属性
-(void)setEmoInfo:(NSDictionary *)emoDic
{
    _emoInfoDic = emoDic;
    
    if ([[emoDic valueForKey:KeyIsNew] boolValue])
    {
        _newImageView.hidden = NO;
    }
        
    //表情封面
    NSString * imageName = @"icon.png";
    NSString * imageUrl = [NSString stringWithFormat:@"%@%@",[_emoInfoDic valueForKey:KeyPath],imageName];
    [_faceImageView setImageWithUrlString:imageUrl withFolderName:[AppComManager remove_:[_emoInfoDic valueForKeyPath:Keyid]] withImageName:imageName];
    
    //设置表情名、简介
    _nameLabel.text = [emoDic valueForKey:KeyName];
    _introductionLabel.text = [emoDic valueForKey:KeyContent1];
    
    [self reloadView];
    
    _mainUrl = [emoDic valueForKey:Keyid];
    
    _peogress.bounds = CGRectMake(0, 0, 50, 20);
    
    if (_btnType == ButtonTypeDownloaded)
    {
        _newImageView.hidden = YES;
    }
    else
    {
        _newImageView.hidden = NO;
    }
}


//下载进度
-(void)setProgress:(float)newProgress
{
    _peogress.progress = newProgress;
}

//下载按钮点击事件
- (IBAction)download:(UIButton *)sender
{
    switch (_btnType)
    {
        case ButtonTypeDownload:
        case ButtonTypeFree://免费表情
        {
            //点击下载表情
            _downloadBtn.hidden = YES;
            _peogress.hidden = NO;
            _peogress.progress = 0;
            
            NSString * emoUrl = [NSString stringWithFormat:@"%@zip.zip",[_emoInfoDic valueForKey:KeyPath]];
            
            [AppComManager downLoadEmoWithUrl:emoUrl withEmoInfoDic:_emoInfoDic delegate:self];
        }
            break;
            
        case ButtonTypeFee://付费
        {
            //去付费
        }
            break;
        default:
            break;
    }
}

-(void)reloadView
{
    NSDictionary * downloadEmoInfoDic = [NSKeyedUnarchiver unarchiveObjectWithFile:DownloadInfoPath];
    downloadEmoInfoDic = [downloadEmoInfoDic valueForKey:[_emoInfoDic valueForKey:Keyid]];
    
    _downloadBtn.hidden = NO;
    _peogress.hidden = YES;
    
    //设置下载按钮
    if ([[downloadEmoInfoDic valueForKey:KeyDownloadState] boolValue])//已下载
    {
        _btnType = ButtonTypeDownloaded;//已下载
        _downloadBtn.hidden = YES;
        self.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else if ([[_emoInfoDic valueForKey:KeyFeeState] boolValue])//付费表情
    {
        _downloadBtn.hidden = NO;
        self.accessoryType = UITableViewCellAccessoryNone;
        
        [_downloadBtn setImage:nil forState:UIControlStateNormal];
        if ([[_emoInfoDic valueForKey:KeyPayState] boolValue])//已付费
        {
            [_downloadBtn setBackgroundImage:[UIImage imageNamed:@"btn_emo_download.png"] forState:UIControlStateNormal];
            [_downloadBtn setTitle:nil forState:UIControlStateNormal];
            _btnType = ButtonTypeDownload;//可下载
        }
        else//未付费
        {
            [_downloadBtn setBackgroundImage:[UIImage imageNamed:@"btn_download_bg.png"] forState:UIControlStateNormal];
            [_downloadBtn setTitle:[_emoInfoDic valueForKey:KeyFee] forState:UIControlStateNormal];
            _btnType = ButtonTypeFee;//未付费
        }
    }
    else//免费表情，未下载
    {
        _downloadBtn.hidden = NO;
        self.accessoryType = UITableViewCellAccessoryNone;
        
        [_downloadBtn setBackgroundImage:[UIImage imageNamed:@"btn_emo_free.png"] forState:UIControlStateNormal];
        [_downloadBtn setTitle:NSLocalizedString(@"free", nil) forState:UIControlStateNormal];
        _btnType = ButtonTypeFree;//可下载
    }
}


@end
